package mammuthus.web.action

import javax.servlet.http.HttpServletRequest

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson.{JSON, JSONObject}
import mammuthus.web.service.{SlaveService, TestConfService}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.{RequestMapping, RequestParam, ResponseBody}

import scala.beans.BeanProperty
import scala.collection.JavaConversions._

/**
 * Created by fengbingjian on 15/5/10 16:14.
 */
@Controller
@RequestMapping(Array("package"))
class PackageManagerAction {

  @Autowired
  @BeanProperty
  var testConfService:TestConfService = null

  @Autowired
  @BeanProperty
  var slaveService:SlaveService = null


  @RequestMapping(Array("addConf.html"))
  def addConf(httpServletRequest: HttpServletRequest,
               @RequestParam(value = "name", defaultValue = "") name: String,
               @RequestParam(value = "url", defaultValue =  "") url: String,
               @RequestParam(value = "host", defaultValue =  "") host: String,
               @RequestParam(value = "user", defaultValue =  "") user: String
      ): String ={


    val r = testConfService.addConf(name, url, host,user)


    val json = new JSONObject()
    json.put("success", r._1)
    json.put("msg", r._2)

    httpServletRequest.setAttribute("result", JSON.toJSONString(json,SerializerFeature.DisableCircularReferenceDetect))
    return "json"
  }



  @RequestMapping(Array("list.html"))
  def list(httpServletRequest: HttpServletRequest,
              @RequestParam(value = "name", defaultValue = "") name: String,
              @RequestParam(value = "url", defaultValue =  "") url: String
               ): String ={


    httpServletRequest.setAttribute("result", testConfService.list())

    httpServletRequest.setAttribute("slave", slaveService.getSlave())

    return "testConfList"
  }


  @RequestMapping(Array("detail.html"))
  def detail(httpServletRequest: HttpServletRequest,
           @RequestParam(value = "name", defaultValue = "") name: String,
           @RequestParam(value = "host", defaultValue =  "") host: String
            ): String ={


    var list = slaveService.listDir("test-" + name, "/", host)
    val installed = if (list.size() == 0) false else true
    httpServletRequest.setAttribute("installed", installed)

    if(installed){
      val module = list.filter(l => !l.isDirectory && l.getName.startsWith("launch_") && l.getName.endsWith(".sh") )
        .map(_.getName.replace("launch_", "").replace(".sh", ""))
      httpServletRequest.setAttribute("module", module)

    }

    httpServletRequest.setAttribute("name", name)

    httpServletRequest.setAttribute("host", host)

    return "testConfDetail"
  }

  @RequestMapping(Array("install.html"))
  @ResponseBody
  def install(httpServletRequest: HttpServletRequest,
                  @RequestParam(value = "name", defaultValue = "") name: String,
                  @RequestParam(value = "host", defaultValue =  "") host: String,
                  @RequestParam(value = "url", defaultValue =  "") url: String
                   ): String = {

    val testName = "test-" + name
    val id = slaveService.shell(testName, host, "shell", "git clone " + url + " " + testName)

    s"""{"id": ${id}}"""
  }


  @RequestMapping(Array("install.html"))
  @ResponseBody
  def process(httpServletRequest: HttpServletRequest,
              @RequestParam(value = "name", defaultValue = "") name: String,
              @RequestParam(value = "host", defaultValue =  "") host: String,
              @RequestParam(value = "action", defaultValue =  "") action: String
               ): String = {

    val testName = "test-" + name

    //val id = slaveService.shell(testName, host, "shell", "git clone " + url + " " + testName)

    s"""{"id": }"""
  }


}
